// Help
// 
// 保佑没Bug 
//
// Created by AS on 2019/4/9. 
//
// Copyright © 2018 AS. All rights reserved.
// 

#import "BarView.h"
@interface BarView()
/**  */
@property (nonatomic,strong) UILabel *label;
/**  */
@property (nonatomic,strong) NSMutableArray<UIImageView *> *imageViews;
/**  */
@property (nonatomic,strong) UIView *ksBgView;
@end

@implementation BarView

-(NSMutableArray *)imageViews{
    if (!_imageViews) {
        _imageViews = [NSMutableArray array];
    }
    return _imageViews;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        [self initSubView];
    }
    return self;
}

-(void)initSubView{
    
     self.ksBgView = [UIView new];
    self.ksBgView.backgroundColor = mMainColor;
    [self.ksBgView setLayerWithRadius:17 color:ClearColor width:0.0];
    [self addSubview:self.ksBgView];
    
    
    for (int i=0;i<10;i++) {
        UIImageView *imageView = [UIImageView new];
        imageView.contentMode = UIViewContentModeScaleToFill;
        imageView.hidden = YES;
        [self addSubview:imageView];
        [self.imageViews addObject:imageView];
    }
    self.label = [UILabel new];
    self.label.font = Font15;
    self.label.textColor = HexRGB(0x222222);
    [self addSubview:self.label];
    
   
    
 
}


-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat margin = ZoomHeight(5);
    CGFloat width = 24;//(self.width - (9 * margin)) / 10;
    for (int i=0;i<self.imageViews.count;i++) {
        UIView *view = self.imageViews[i];
        if ([self.model.code isEqualToString:@"k3"]) {
            view.frame = CGRectMake(15 +i * margin + (i * width),0, width, width);
        }else{
            view.frame = CGRectMake(i * margin + (i * width),0, width, width);
        }
        
    }
    CGFloat x = 0;
    if ([self.model.code isEqualToString:@"ssc"]) {
        x = 5 * margin + (5 * width) + 5;
        CGRect frame =  CGRectMake(x, 0, self.width - x, 24);
        self.label.frame = frame;
        self.label.centerY = self.imageViews[0].centerY;
    }else if ([self.model.code isEqualToString:@"k3"]){
        x = 3 * margin + (3 * width) + 5 + 30;
        CGRect frame =  CGRectMake(x, 0, self.width - x, 24);
        self.label.frame = frame;
        self.label.centerY = self.imageViews[0].centerY;
        self.ksBgView.hidden = NO;
        self.ksBgView.frame = CGRectMake(0, -5,2 * margin + (3 * width) + 30,34);
    }
    
    
}

-(void)setModel:(MModel *)model{
    _model = model;
    for (UIView *view in self.subviews) {
        view.hidden = YES;
    }
    
    NSString *path = [PathDocument stringByAppendingPathComponent:@"ionic/www/assets/gameIcon"];
    NSInteger sum = 0;
    for (int i=0; i<model.openNumbers.count; i++) {
        NSInteger number = [model.openNumbers[i] integerValue];
        UIImageView *imageView = self.imageViews[i];
        imageView.hidden = NO;
        NSString *imageName = @"";
        sum += number;
        if([model.code isEqualToString:@"k3"]){
            imageName = [NSString stringWithFormat:@"ks%ld.png",number];
        }else if([model.code isEqualToString:@"pk10"]){
            imageName = [NSString stringWithFormat:@"pk%ld.png",number];
        }else if([model.code isEqualToString:@"lhc"]){
            imageName = [NSString stringWithFormat:@"lh%@.png",model.openNumbers[i]];
        }else{
            imageName = [NSString stringWithFormat:@"%ld.png",number];
        }
        NSString *imagePath = [NSString stringWithFormat:@"%@/%@", path,imageName];
        UIImage *image = [[UIImage alloc]initWithContentsOfFile:imagePath];
        if (!image) {
            NSLog(@"%@",imageName);
        }
        imageView.image = image;
    }
    if([model.code isEqualToString:@"k3"]){
        self.label.hidden = NO;
        NSString *type = sum > 10 ? @"大" : @"小";
        NSString *type1 = sum % 2 == 0 ? @"双" : @"单";
        NSString *string = [NSString stringWithFormat:@"和值: %ld  %@%@",sum,type,type1];
        self.label.text = string;
    }else if([model.code isEqualToString:@"ssc"]){
        self.label.hidden = NO;
        NSString *string = [self niuniu:[NSMutableArray arrayWithArray:model.openNumbers]];
        NSString *type = sum >= 23 ? @"大" : @"小";
        NSString *type1 = sum % 2 == 0 ? @"双" : @"单";
        self.label.text = [NSString stringWithFormat:@"%@%@ | %@",type,type1,string];;

    }
    [self layoutSubviews];
}


-(NSString *)niuniu:(NSMutableArray *)array{
    int index,index1,index2;
    
    for (int i=0; i<array.count - 2; i++) {
        int value2 = [array[i] intValue];
        index = i;
        for (int j=i+1; j<array.count - 1; j++) {
            index1 = j;
            int value1 = [array[j] intValue];
            for (int k=j+1; k<array.count; k++) {
                int value = [array[k] intValue];
                index2 = k;
                if ((value + value1 + value2) % 10 == 0) {
                    [array removeObjectAtIndex:index];
                    [array removeObjectAtIndex:index1 - 1];
                    [array removeObjectAtIndex:index2 - 2];
                    int sum = [array[0] intValue] + [array[1] intValue];
                    return [NSString stringWithFormat:@"牛%@",[self dy:sum % 10]];
                }
            }
        }
    }
    return @"无牛";
}

-(NSString *)dy:(int)num{
    
    switch (num) {
        case 1:
            return @"一";
        case 2:
            return @"二";
        case 3:
            return @"三";
        case 4:
            return @"四";
        case 5:
            return @"五";
        case 6:
            return @"六";
        case 7:
            return @"七";
        case 8:
            return @"八";
        case 9:
            return @"九";
        default:
            return @"牛";
    }
    
    
}




@end

